const {
  config
} = require('../../../utils/config.js')
const app = getApp()

Page({
  data: {
    imgs : [],
    title : '',
    info : '',
    size : 0,
    point : 0,
    stock : 0,
    id : null,
    stock: 0
  },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      point : options.point
    })
    this.getGoodsInfo(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  getGoodsInfo(id){
    let that = this
    wx.request({
      url: `${config}/wechat/integralGoods/goodsInfo`,
      data: {
        goodsId:id
      },
      success(resp) {
        console.log(resp);
        that.setData({
          imgs : resp.data.data.goods_images,
          title : resp.data.data.goods_name,
          info : resp.data.data.goods_description,
          size : resp.data.data.goods_integral,
          stock : resp.data.data.goods_stock,
          id : resp.data.data.goods_id,
          stock : resp.data.data.goods_stock
        })
      
        // 隐藏加载框
        wx.hideLoading();
      }
    })
  },

  toHomePage(){
    wx.navigateTo({
      url: '/pages/exchangerecord/exchangerecord'
    })
  },

  buyNow(){
    let that = this
    wx.showModal({
      cancelColor: 'cancelColor',
      title: '提示',
      content: '将要兑换'+this.data.title+',扣除'+this.data.size+'积分',
      success(res){
        if(res.confirm){
          if(that.data.size > that.data.point){
            wx.showModal({
              cancelColor: 'cancelColor',
              title: '提示',
              content: '积分不足，记得努力学习赚取积分喔~',
              showCancel:false
            })
            return
          }
          if(that.data.stock < 1){
            wx.showModal({
              cancelColor: 'cancelColor',
              title: '提示',
              content: '库存不足',
            })
            return
          }
          wx.request({
            url: `${config}/erp/erpWeChat/erpwechatintegralrecord/exchangeGoods`,
            data:{
              goodsId: that.data.id,
              goodsName: that.data.title,
              goodsIntegral: that.data.size,
              goodsStock: that.data.stock,
              stuId: app.globalData.userInfo.id,
              stuIntegral: that.data.point
            },
            success(resp){
              if(resp.data.flag == true){
                wx.showToast({
                  title: '兑换成功!',
                })
                setTimeout(function(){
                  wx.navigateTo({
                    url: '/pages/exchangerecord/exchangerecord?status='+0,
                  })
                },1500)
              }else{
                wx.showToast({
                  title: '兑换失败',
                  icon: 'error'
                })
              }
            }
          })
        }else{
        }
      }
    })
  }

})